varying vec3 out_norm;
varying vec3 out_pos;
varying float out_material;

//uniform vec3 material_ka[500];
//uniform vec3 material_kd[500];
//uniform vec3 material_ks[500];

//uniform vec3 material_ka[10];
//uniform vec3 material_kd[10];
//uniform vec3 material_ks[10];

uniform sampler2D material_kakdks;
uniform float material_count;

vec3 material_color(float color_type)
{
    float material_offset = (out_material + 0.5)/material_count;
    float material_selector = (color_type + 0.5)/3.0;
    vec4 as_rgba = texture2D(material_kakdks, vec2(material_offset, material_selector));

    return vec3(as_rgba);
}

void main(void)
{
    vec3 dir_light_dir = vec3(-1.0, -1.0, -1.0);
//    vec3 dir_light_base_color = material_ka[int(out_material)];
    vec3 dir_light_base_color = material_color(0.0);

    float shade_level_dir = abs(dot(dir_light_dir, out_norm));
    vec3 shaded_color_dir = dir_light_base_color*shade_level_dir;

    vec3 dir_light_dir2 = vec3(1.0, 1.0, -1.0);
//    vec3 dir_light_base_color2 = material_kd[int(out_material)];
    vec3 dir_light_base_color2 = material_color(1.0);

    float shade_level_dir2 = cos(1.0 - abs(dot(dir_light_dir2, out_norm)))*1.5;
    vec3 shaded_color_dir2 = dir_light_base_color2*shade_level_dir2*shade_level_dir2*shade_level_dir2;

//    vec3 extra_color = material_ks[int(out_material)];
    vec3 extra_color = material_color(2.0);

    vec3 shaded_color = shaded_color_dir*0.2
            + shaded_color_dir2*0.3
            + extra_color*0.25;

    gl_FragColor = vec4(shaded_color, 1.0);
}

//void main(void)
//{
//    vec3 dir_light_dir = vec3(-1.0, -1.0, -1.0);
//    vec3 dir_light_base_color = vec3(0.3, 0.3, 0.4);

//    float shade_level_dir = abs(dot(dir_light_dir, out_norm));
//    vec3 shaded_color_dir = dir_light_base_color*shade_level_dir;

//    vec3 dir_light_dir2 = vec3(1.0, 1.0, -1.0);
//    vec3 dir_light_base_color2 = vec3(0.3, 0.3, 0.4);

//    float shade_level_dir2 = cos(1.0 - abs(dot(dir_light_dir2, out_norm)))*1.5;
//    vec3 shaded_color_dir2 = dir_light_base_color2*shade_level_dir2*shade_level_dir2*shade_level_dir2;

//    vec3 extra_color = vec3(0.1, 0.1, 0.1);

//    vec3 shaded_color = shaded_color_dir*1.2
//            + shaded_color_dir2*0.4
//            + extra_color;

//    gl_FragColor = vec4(shaded_color, 1.0);
//    //    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
//}
